/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateless;

import java.sql.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import persistence.entity.Accounttransaction;
import persistence.entity.Account;

/**
 *
 * @author Craig
 */
@Stateless
public class AccounttransactionFacade extends AbstractFacade<Accounttransaction> implements AccounttransactionFacadeRemote {
    @PersistenceContext(unitName = "Entity-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public AccounttransactionFacade() {
        super(Accounttransaction.class);
    }

    @Override
    public Accounttransaction createTransaction(char transType, int transAmount, String transDesc, Account fromAccount, Account toAccount) {
        Accounttransaction accTrans = new Accounttransaction();
        accTrans.setTransType(transType);
        accTrans.setTransDate(new Date(new java.util.Date().getTime()));
        accTrans.setTransAmount(transAmount);
        accTrans.setTransDescription(transDesc);
        accTrans.setTransFromAccount(fromAccount);
        accTrans.setTransToAccount(toAccount);
        
        try {
            create(accTrans);
            List<Accounttransaction> all = findAll();
            Accounttransaction lastAdded = all.get(all.size() - 1);
            return lastAdded;
        } catch (Exception e) {
            System.err.println(e.getMessage());
            return null;
        }
    }
    
}
